<?php

namespace app\admin\model;

use app\model\Model;

class Slideshow extends Model
{
  public $privilege = 'slideshow';
  public $cids;
  public $id;
  public $res;
  public $db;
  public $s_parent_array = [];

  function __construct()
  {
    include 'check.php';
    new Check();

    $this->db = $GLOBALS['db'];
    $this->cids = checkAdminPriv(@$this->privilege); // 判断权限
    $this->id = intval($_GET['id']);
    $this->res = file_get('json', 'slideshow');
    $this->res = array_sort($this->res, 's_order');

    $GLOBALS['cms']['editor_upload'] = array(array('image', '#s_picture_upload'));
  }

  function index()
  {
    $this->tpl('slideshow', [
      'list' => $this->res,
      'db' => $this->db
    ]);
  }

  function add()
  {
    $data = get_post(['s_link']);

    $data['s_link'] = $_POST['s_link'] != 'http://' ? str_safe($_POST['s_link']) : '';

    $this->res[] = $data;
    $this->res = array_sort($this->res, 's_order');

    if (file_it(json_encode($this->res, 320), 'json', 'slideshow')) {
      admin_log('幻灯新增');
      alert_href($GLOBALS['lang']['msg_success'], M('admin', 'slideshow'));
    }
    alert_href($GLOBALS['lang']['msg_tryagain'], M('admin', 'slideshow'));

    /*
    $data['s_name'] = str_safe($_POST['s_name']);
    $data['s_parent'] = str_safe($_POST['s_parent']);
    $data['s_picture'] = str_safe($_POST['s_picture']);
    $data['s_order'] = intval($_POST['s_order']);


    null_back($data['s_picture'], '图片不能为空');
    non_numeric_back($data['s_order'], '排序必须是数字!');
    */
  }

  function edit()
  {
    $this->tpl('slideshow_edit', [
      'row' => $this->res[$this->id],
      'db' => $this->db
    ]);
  }

  function edit_action()
  {
    $data = get_post(['s_link']);

    $this->res[$this->id] = $data;
    $this->res = array_sort($this->res, 's_order');

    if (file_it(json_encode($this->res, 320), 'json', 'slideshow')) {
      admin_log('幻灯编辑 [' . $this->id . ']');
      alert_href($GLOBALS['lang']['msg_success'], M('admin', 'slideshow'));
    }

    alert_href($GLOBALS['lang']['msg_tryagain'], M('admin', 'slideshow'));

    /*
    $data['s_name'] = str_safe($_POST['s_name']);
    $data['s_parent'] = str_safe($_POST['s_parent']);
    $data['s_picture'] = str_safe($_POST['s_picture']);
    $data['s_link'] = str_safe($_POST['s_link']);
    $data['s_order'] = intval($_POST['s_order']);

    null_back($data['s_picture'], '图片不能为空');
    non_numeric_back($data['s_order'], '排序必须是数字!');
    */
  }

  function del()
  {
    unset($this->res[$this->id]);
    $this->res = array_sort($this->res, 's_order');

    if (file_it(json_encode($this->res, 320), 'json', 'slideshow')) {
      admin_log('幻灯删除 [' . $this->id . ']');
      alert_href($GLOBALS['lang']['msg_success'], M('admin', 'slideshow'));
    }

    alert_href($GLOBALS['lang']['msg_tryagain'], M('admin', 'slideshow'));
  }

  function __destruct()
  {
  }
}
